// subpkg/order/order.js
import { addOrder } from '../../api/order'
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    movieBuyInfo: {}
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      movieBuyInfo: app.globalData.movieBuyInfo
    })
  },
  async goToPay() {
    const token = wx.getStorageSync('token')
    if (!token) {
      // 未登录，跳转到登录页面
      wx.navigateTo({
        url: '/subpkg/login/login?needBack=true',
      })
    } else {
      // 下单
      const data = {
        cinema: this.data.movieBuyInfo.cinemaName,
        moviename: this.data.movieBuyInfo.nm,
        movieimg: this.data.movieBuyInfo.img,
        hall: this.data.movieBuyInfo.th,
        status: 2,
        paytime: `${this.data.movieBuyInfo.dt} ${this.data.movieBuyInfo.tm}`,
        totalPrice: this.data.movieBuyInfo.totalPrice,
        count: this.data.movieBuyInfo.count,
        seatList: this.data.movieBuyInfo.seatList
      }

      // 发请求
      const res = await addOrder(data)
      wx.showToast({
        title: '支付成功',
        icon: 'success',
        duration: 800,
        success: () => {
          setTimeout(() => {
            wx.navigateTo({
              url: '/subpkg/order-list/order-list'
            })
          }, 800)
        }
      })
    }
  }
})